<template>
  <div class="login_container">
    <Transition name="slide-fade">
      <div v-if="flag == false" class="login_box">
        <!-- 头像区域 -->
        <div class="avatar_box">
          <img src="@/assets/img/logo.png" alt="加载中..." />
        </div>
        <!-- 标题 -->
        <h1 class="title">{{ t('login') }}</h1>
        <!-- 表单区域 -->
        <el-form :model="loginStore.loginParams" :rules="loginFormRules" ref="loginFormRef" label-width="0px"
          class="login_form">
          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input v-model="loginStore.loginParams.username" prefix-icon="User" placeholder="admin">
            </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="loginStore.loginParams.password" prefix-icon="Lock" type="password" placeholder="123456"
              show-password>
            </el-input>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary" @click="login">{{ t('login') }}</el-button>
            <el-button type="info" @click="reset_loginForm">{{ t('reset') }}</el-button>
            <a @click="forgetPassword">{{ t('forgotPassword') }}</a>
          </el-form-item>
        </el-form>
        <div class="toolbar">
          <!-- 主题切换 -->
          <SwitchThemeButton />
          <!-- 语言国际化组件 -->
          <LanguageSelector />
          <!-- 全屏组件 -->
          <FullScreenButton />
        </div>
      </div>
      <div v-else class="login_box">
        <!-- 头像区域 -->
        <div class="avatar_box">
          <img src="@/assets/img/logo.png" alt="加载中..." />
        </div>
        <!-- 标题 -->
        <h1 class="title">{{ t('forgotPassword') }}</h1>
        <!-- 表单区域 -->
        <el-form label-width="0px" class="login_form">
          <!-- 账号 -->
          <el-form-item>
            <el-input prefix-icon="User" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <!-- 新密码 -->
          <el-form-item>
            <el-input prefix-icon="Lock" placeholder="请输入新密码">
            </el-input>
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item>
            <el-input prefix-icon="Lock" placeholder="请输入验证码">
            </el-input>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary">确认重置</el-button>
            <a @click="forgetPassword">前往登录</a>
          </el-form-item>
        </el-form>
        <div class="toolbar">
          <!-- 主题切换 -->
          <SwitchThemeButton />
          <!-- 语言国际化组件 -->
          <LanguageSelector />
          <!-- 全屏组件 -->
          <FullScreenButton />
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup>
import LanguageSelector from '@/components/languageSelector.vue'; // 引入语言组件
import SwitchThemeButton from '@/components/switchThemeButton.vue';// 引入主题切换组件
import FullScreenButton from '@/components/fullScreenButton.vue';// 引入全屏组件
import { ref, reactive } from "vue";
import useStore from "@/pinia/index.js";
import { useI18n } from 'vue-i18n';

const { loginStore } = useStore();
const { t } = useI18n();// 引入国际化组件动态翻译方法
// 表单对象
const loginFormRef = ref(null);
// 表单验证规则
const loginFormRules = reactive({
  username: [
    { required: true, message: t('userPlaceholder'), trigger: "blur" },
    { min: 3, max: 10, message: t('usernameTips'), trigger: "blur" },
  ],
  password: [
    { required: true, message: t('passwordPlaceholder'), trigger: "blur" },
    { min: 6, max: 12, message: t('passwordTips'), trigger: "blur" },
  ],
});
// 重置表单
const reset_loginForm = () => {
  loginFormRef.value.resetFields();//重置表单内容
};
// 登录
const login = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loginStore.REQUEST_login();//登录请求,获取token,联动路由进行跳转
    } else {
      return false
    }
  });
};
// 忘记界面
let flag = ref(false);
// 切换页面
const forgetPassword = () => {
  flag.value = !flag.value;
}

</script>

<style lang="less" scoped>
// 旋转动画
.slide-fade-enter-active {
  transition: all 1s ease;
}

.slide-fade-leave-active {
  transition: all 1s ease;
}

.slide-fade-enter-from,
.slide-fade-leave-to {
  transform: rotateY(180deg);
  opacity: 0;
}

.title {
  font-size: 20px;
  font-weight: bold;
  position: absolute;
  left: 0;
  top: 0;
  margin: 20px;
  user-select: none;
}

.login_container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg,
      hsl(170deg, 80%, 70%),
      hsl(190deg, 80%, 70%),
      hsl(250deg, 80%, 70%),
      hsl(320deg, 80%, 70%));
  background-size: 200% 200%;
  animation: gradient-move 5s ease alternate infinite;

  @keyframes gradient-move {
    0% {
      background-position: 0% 0%;
    }

    100% {
      background-position: 100% 100%;
    }
  }

  .login_box {
    width: 400px;

    @media (max-width: 400px) {
      width: 90%;
    }

    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    box-shadow: rgba(0, 0, 0, 0.3) 0px 19px 38px,
    rgba(0, 0, 0, 0.22) 0px 15px 12px;

    .avatar_box {
      width: 100px;
      height: 100px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
        user-select: none; //禁止图片拖动
      }
    }

    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;

      a {
        position: absolute;
        right: 0;
        bottom: 0;

        &:hover {
          cursor: pointer;
          color: #409EFF;
          border-bottom: 1px solid #409EFF;
        }
      }


    }
  }
}


.toolbar {
  display: grid; // 定义网格布局
  grid-template-columns: 1fr 1fr 1fr; // 列定义
  grid-gap: 10px; // 列间距
  align-items: center; // 水平居中
  margin: 20px;
  position: absolute;
  right: 0;
  top: 0;
}
</style>
